package com.shaoyuwang.util;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

// utilize
public class DBUtil {
    // 由于 MysqlDataSource 对象只需要一份，所以，一个 static 就够了
    private static final DataSource dataSource;

    // 类加载时执行
    static {
        MysqlDataSource db = new MysqlDataSource();
        db.setServerName("localhost");
        db.setPort(3306);
        db.setUser("root");
        db.setPassword("123456");
        db.setDatabaseName("lib_0403");
        db.setUseSSL(false);
        db.setCharacterEncoding("utf-8");
        db.setServerTimezone("Asia/Shanghai");

        dataSource = db;
    }

    // 返回Connection对象
    public static Connection connection() throws SQLException {
        return dataSource.getConnection();
    }
}
